<?php
namespace app\adm\controller;
use think\Controller;
use think\Db;
use think\Request;
class Gongyi extends Index
{
	public function __construct(Request $request){
		parent::__construct($request);
	}
	/**
	* 后台公益列表
	*/
	public function gylist(){
		$page = Db::name('gongyi')->paginate(8,false,['type' => 'bootstrap','var_page' => 'page',]);
		$gylist = $page->all();
		foreach ($gylist as $key => $value) {
			$gylist[$key]['nickname'] = Db::name('user')->where(['userid'=>$gylist[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('gylist',$gylist);
		return $this->fetch('gylist',['title'=>'后台公益列表']);
	}
	/**
	* 发表后台公益
	*/
	public function gyadd(){
		// return view('gyadd');
		return $this->fetch('gyadd',['title'=>'后台公益添加']);
	}
	/*
	* 添加公益操作
	*/
	public function gyInsert()
	{
		if(request()->isPost()){
			if(input('post.gytitle')=="" || input('post.gycontent')==''){
				jsBack('标题和内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('gyphoto');
			if($file){
				//移动到框架应用根目录/public/uploads/gongyi目录下
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/gongyi');
				//上传成功后获取后台公益信息
				if($info){
					$data['gyphoto']='/public/uploads/gongyi/'.date("Ymd").'/'.$info->getFilename();
					$data['gytitle']=input('post.gytitle');
					$data['gycontent']=input('post.gycontent');
					$data['addtime']=time();
					$data['userid']=session('userid');
					//后台公益类型为1
					$data['gytype']='1';
					$gongyi = Db::name('gongyi')->insert($data);
					//公益添加
					if($gongyi){
						jsBack('添加成功','/adm/gongyi/gylist');
					}else{
						jsBack('网络有误,请重试');
					}
				}else{
					//输出错误信息
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择公益展示图');
			}
		}else{
			$this->redirect('gongyi/gyadd');
		}
	}
	//删除公益操作
	public function del_gy(){
		$gyid = input('gyid');
		$res = Db::name('gongyi')->delete($gyid);
		if($res){
			jsBack('删除成功');
		}else{
			jsBack('删除失败');
		}
	}
	/**
	* 后台公益修改
	*/
	public function gyedit()
	{
		$where['gyid']=input('gyid');
		$gongyi = Db::name('gongyi')->where(['gyid'=>input('gyid')])->find();
		$this->assign('gongyi',$gongyi);
		return $this->fetch('gyedit',['title'=>'后台公益修改']);
	}
	public function gyUpdate()
	{
		if(request()->isPost()){
			if(input('post.gycontent')==''){
				jsBack('内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('gyphoto');
			if($file){
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/gongyi');
				if($info){
					$data['gyphoto'] = '/public/uploads/gongyi/'.date("Ymd").'/'.$info->getFilename();
					$data['gycontent']=input('post.gycontent');
					$data['addtime']=time();
					$gyUpdate = Db::name('gongyi')->where(['gyid'=>input('gyid')])->update($data);
					if($gyUpdate){
						jsBack('修改成功','/adm/gongyi/gylist');
					}else{
						jsBack('修改失败');
					}
				}else{
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择公益展示图');
			}
		}else{
			$this->redirect('gongyi/gyedit');
		}
	}


	/**
	* 捐助列表
	*/
	public function helplist(){
		$page = Db::name('help')->paginate(8,false,['type' => 'bootstrap','var_page' => 'page',]);
		$helplist = $page->all();
		foreach ($helplist as $key => $value) {
			$helplist[$key]['nickname'] = Db::name('user')->where(['userid'=>$helplist[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('helplist',$helplist);
		return $this->fetch('helplist',['title'=>'后台公益列表']);
	}
	/**
	* 发表后台捐助
	*/
	public function helpadd(){
		// return view('helpadd');
		return $this->fetch('helpadd',['title'=>'后台捐助添加']);
	}
	/*
	* 添加捐助操作
	*/
	public function helpInsert()
	{
		if(request()->isPost()){
			if(input('post.helptitle')=="" || input('post.helpcontent')==''){
				jsBack('标题和内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('helpphoto');
			if($file){
				//移动到框架应用根目录/public/uploads/gongyi目录下
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/gongyi');
				//上传成功后获取后台捐助信息
				if($info){
					$data['helpphoto']='/public/uploads/gongyi/'.date("Ymd").'/'.$info->getFilename();
					$data['helptitle']=input('post.helptitle');
					$data['helpcontent']=input('post.helpcontent');
					$data['addtime']=time();
					$data['userid']=session('userid');
					//后台捐助类型为1
					$data['helptype']='1';
					$help = Db::name('help')->insert($data);
					//捐助添加
					if($help){
						jsBack('添加成功','/adm/gongyi/helplist');
					}else{
						jsBack('网络有误,请重试');
					}
				}else{
					//输出错误信息
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择捐助展示图');
			}
		}else{
			$this->redirect('gongyi/helpadd');
		}
	}
	//删除捐助操作
	public function del_help(){
		$helpid = input('helpid');
		$res = Db::name('help')->delete($helpid);
		if($res){
			jsBack('删除成功');
		}else{
			jsBack('删除失败');
		}
	}
	/**
	* 后台捐助修改
	*/
	public function helpedit()
	{
		$where['helpid']=input('helpid');
		$help = Db::name('help')->where(['helpid'=>input('helpid')])->find();
		$this->assign('help',$help);
		return $this->fetch('helpedit',['title'=>'后台捐助修改']);
	}
	public function helpUpdate()
	{
		if(request()->isPost()){
			if(input('post.helpcontent')==''){
				jsBack('内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('helpphoto');
			if($file){
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/gongyi');
				if($info){
					$data['helpphoto'] = '/public/uploads/gongyi/'.date("Ymd").'/'.$info->getFilename();
					$data['helpcontent']=input('post.helpcontent');
					$data['addtime']=time();
					$helpUpdate = Db::name('help')->where(['helpid'=>input('helpid')])->update($data);
					if($helpUpdate){
						jsBack('修改成功','/adm/gongyi/helplist');
					}else{
						jsBack('修改失败');
					}
				}else{
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择捐助展示图');
			}
		}else{
			$this->redirect('gongyi/helpedit');
		}
	}
}